package com.focusai.efairy.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.focusai.efairy.R;


/**
 * 子元素的EditText设置android:background="@null"，已设置左右padding=12dp
 * Created by cwf on 15/7/7.
 */
public class LoginLinearLayout extends LinearLayout implements
        View.OnFocusChangeListener {

    public static final int STATUS_FOCUSED = 1;
    public static final int STATUS_UNFOCUSED = 2;
    private int status = 2;

    private int focusedLeftDrawableId = -1, focusedRightDrawableId = -1;// 默认的左边icon
    private int unfocusedLeftDrawableId = -1, unfocusedRightDrawableId = -1;//获取焦点后的左边icon
    private Context mContext;


    private ImageView mLeftIcon, mRightIcon;
    private EditText mEditText;//处理焦点的输入框

    public LoginLinearLayout(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public LoginLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();

    }

    public LoginLinearLayout(Context context, AttributeSet attrs, int defStryle) {
        super(context, attrs);
        mContext = context;
        init();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int count = getChildCount();
        if (count > 0) {
            View child;
            for (int i = 0; i < count; i++) {
                child = getChildAt(i);
                if (child instanceof EditText) {
                    mEditText = (EditText) child;
                } else if (child instanceof ImageView) {
                    if (mLeftIcon == null) {
                        mLeftIcon = (ImageView) child;
                    } else if (mRightIcon == null) {
                        mRightIcon = (ImageView) child;
                    }
                }
            }
        }
        addListeners();
        setPadding(mContext.getResources().getDimensionPixelSize(R.dimen.common_margin_dz1), 0, mContext.getResources().getDimensionPixelSize(R.dimen.common_margin_dz1), 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    private void init() {
        setStatus(status);
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
    }

    public void setStatus(int status) {
        this.status = status;
        if (status == STATUS_FOCUSED) {
            if (mLeftIcon != null && focusedLeftDrawableId != -1) {
                mLeftIcon.setImageResource(focusedLeftDrawableId);
            }
            if (mRightIcon != null && focusedRightDrawableId != -1) {
                mRightIcon.setImageResource(focusedRightDrawableId);
            }
            setBackgroundResource(R.drawable.login_linear_shape_focus);
        } else {
            if (mLeftIcon != null && unfocusedLeftDrawableId != -1) {
                mLeftIcon.setImageResource(unfocusedLeftDrawableId);
            }
            if (mRightIcon != null && unfocusedRightDrawableId != -1) {
                mRightIcon.setImageResource(unfocusedRightDrawableId);
            }
            setBackgroundResource(R.drawable.login_linear_shape);
        }
        invalidate();
    }

    public void setLeftDrawable(int focusedDrawableId, int unfocusedDrawableId) {
        this.focusedLeftDrawableId = focusedDrawableId;
        this.unfocusedLeftDrawableId = unfocusedDrawableId;
        setStatus(status);
    }

    public void setRightDrawable(int focusedDrawableId, int unfocusedDrawableId) {
        this.focusedRightDrawableId = focusedDrawableId;
        this.unfocusedRightDrawableId = unfocusedDrawableId;
        setStatus(status);
    }

    private void addListeners() {
        try {
            if (mEditText != null) {
                mEditText.setOnFocusChangeListener(this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
                                  Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            setStatus(STATUS_FOCUSED);
        } else {
            setStatus(STATUS_UNFOCUSED);
        }
    }

    @Override
    public void onFocusChange(View arg0, boolean arg1) {
        // TODO Auto-generated method stub
        if (arg1) {
            setStatus(STATUS_FOCUSED);
        } else {
            setStatus(STATUS_UNFOCUSED);
        }
    }
}